﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


using System.Collections.Generic;

using Jzy.BLL.DocManger;
using Frame.Lib.Core.Right;
using Frame.Lib.Enums;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Jzy.DAL;


public partial class DocManger_MoveDlg : PageSelectTree
{
    private DirBO _dir;
    private DirBO Dir
    {
        get
        {
            if (_dir == null)
            {
                _dir = new DirBO();
            }
            return _dir;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTree();
        }
        AddAttr();
    }

    private void AddAttr()
    {
        tvList.Attributes.Add("onclick", "SingleSelect();");
    }

    private void BindTree()
    {
        if (AppSession.UserID == Constants.ADMIN_ID)
        {
            Dir.BindTreeView(tvList, null,true);
        }
        else
        {
            RecordRightBO right = new RecordRightBO(OTableName.DocDirRight);
            List<int> lstDirIDs = right.GetBindIDs(AppSession.UserID, RecordRightName.Browse, RightOwnerType.User);
            Dir.BindTreeView(tvList, lstDirIDs,true);

            //对有增加权限的加上CheckBox
            List<int> lstAddIDs = right.GetBindIDs(AppSession.UserID, RecordRightName.Add, RightOwnerType.User);
            if (tvList.Nodes.Count > 0)
            {
                SetCheckBox(tvList.Nodes[0], lstAddIDs);
            }
        }
    }

    private void SetCheckBox(TreeNode treeNode, List<int> lstAddIDs)
    {
        int id = int.Parse(treeNode.Value);
        if (lstAddIDs.IndexOf(id) >= 0)
        {
            treeNode.ShowCheckBox = true;
        }
       
        foreach (TreeNode node in treeNode.ChildNodes)
        {
            SetCheckBox(node, lstAddIDs);
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        if (tvList.CheckedNodes.Count > 0)
        {
            int dirID = Convert.ToInt32(tvList.CheckedNodes[0].Value);

            //移动目录
            string strSelectDirIDs = Request["SelectDirIDs"];
            List<int> lstSelectDirIDs = StrUtil.SplitToInt(strSelectDirIDs);
            if (Dir.Include(lstSelectDirIDs, dirID))
            {
                JSUtil.Alert("目标目录不得包括自己及子目录");
                return;
            }
            Dir.Move(lstSelectDirIDs, dirID);
            string strScript = "";

            //移动文档
            string strSelectDocIDs = Request["SelectDocIDs"];
            List<int> lstSelectDocIDs = StrUtil.SplitToInt(strSelectDocIDs);
            DocumentBO doc = new DocumentBO();
            doc.Move(lstSelectDocIDs, dirID);

            strScript += "close();returnValue=true";
            JSUtil.ResponseJS(strScript);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        JSUtil.ResponseJS("close();returnValue=false");
    }
}
